package thomas.studio.spring.security.ldap.service;

import org.springframework.ldap.core.DirContextOperations;
import org.springframework.security.ldap.userdetails.LdapUserDetails;
import org.springframework.security.ldap.userdetails.LdapUserDetailsImpl;

public class User extends LdapUserDetailsImpl{
	
	private static final long serialVersionUID = -903206904770597340L;

	private String realname;
	
	private String mail;
	
	public User() {		
		super();
	}

	public String getRealname() {
		return realname;
	}
	
	public String getMail() {
		return mail;
	}

		public static class Essence extends LdapUserDetailsImpl.Essence{
			public Essence(){
				super();
			}
			
			public Essence(DirContextOperations context){
				super(context);
				this.setRealname(context.getStringAttribute("cn"));
				this.setMail(context.getStringAttribute("mail"));
			}
			
			public Essence(User user){
				super(user);
				this.setRealname(user.getRealname());
				this.setMail(user.getMail());
			}
			
			/**
			 * make sure (User)this.instance could work 
			 */
			@Override
			protected final LdapUserDetailsImpl createTarget(){
				return new User();
			}
			
			
			@Override
			public LdapUserDetails createUserDetails(){
				//LdapUserDetails userDetails = super.createUserDetails();
				User user = (User) super.createUserDetails();
				return user;
			}
			
			public void setMail(String mail) {
				((User)this.instance).mail = mail;
			}
			
			public void setRealname(String realname) {
				((User)this.instance).realname = realname;
			}

		}
	
}
